$('document').ready( function(){

  /*
   * Slight fade out effect on main navigation dropdowns
   */
  $('#nav > li').hover(function(){
    $('#nav ul').hide();
    $(this).find('> ul').stop(true, true).css({display: 'block', opacity: 1});
  }, function(){
    $(this).find('> ul').stop(true, true).delay(400).fadeOut(300);
  });

  /*
   * Slideshows and carousels
   */
  $('.flexslider').each(function(){

    var slideOptions = {};

    // Carousel
    if( $(this).hasClass('carousel') ) {
      slideOptions = {
        animation: 'slide',
        slideshow: false,
        animationSpeed: 600,
        directionNav: false
      };
    // Hero slideshows
    } else {
      slideOptions = {
        animation: 'fade',
        slideshow: true, // automatically start
        slideshowSpeed: 8000,
        animationSpeed: 1000,
        directionNav: false,
        initDelay: 1000
      };
    }
    $(this).flexslider(slideOptions);

  });

  /*
   * Video lighbox
   */
  $('a.lightbox-video').fancybox({
    type: 'iframe',
    autosize: false,
    width: 640,
    height: 360,
    scrolling: 'no'
  });

  /*
   * Image lighbox
   */
  $('a.zoom').fancybox();

  /*
   * Open sidebar form in lightbox
   */
  $('a[href="#mini-form"]').each(function(){
    
    var formcopy = $($(this).attr('href')).clone();

    $(this).fancybox({
      href: null,
      content: formcopy,
      scrolling: 'no'
    });
  });

  /*
   * Global websites jump lists
   */
  $('select.global-websites').change(function(){
    
    var url = $(this).val();

    if(!url) return;

    window.location = url;

  });

  /*
   * Contact page select list toggle
   */
  $('select.contact-toggle').change(function(){
    
    var selector = $(this).val();

    $('.contact-results > li').hide();

    if(selector != '') {
      $('.contact-results > li'+selector).show();
    }

  });

  /*
   * Contact page tabs
   */

  $('.tab-nav a').each(function(){

    var $nav = $(this).parent().parent();

    if( ! $(this).parent().hasClass('active')) {
      $($(this).attr('href')).hide();
    }

    $(this).click(function(e){
      e.preventDefault();

      if( ! $(this).parent().hasClass('active')) {

        $nav.find('a').each(function(){
          $($(this).attr('href')).hide();
          $(this).parent().removeClass('active');
        });

        $(this).parent().addClass('active');
        $($(this).attr('href')).show();
      
      }
    })
  });

  /*
   * Landing page thumbnail hover fx
   */
  $('.thumbnail').hover(function(){
    $(this).find('.text').stop().animate({top: '0px'}, 300);
  }, function(){
    $(this).find('.text').stop().animate({top: '149px'}, 300);
  });

  /*
   * Resource Library expand collapse
   */
  $('.resource-links').each(function(){
    
    var $list = $(this);

    // limit to twelve items if there are more
    if( $list.find('> li').length > 12) {

      $list.addClass('closed').find('> li:gt(11)').addClass('hidden');

      var $toggle = $('<p class="resource-toggle"><a href="#" class="more">View All</a></p>').insertAfter($list);

      $toggle.click(function(e){
        e.preventDefault();

        if($list.hasClass('closed')) {
          $list.removeClass('closed').find('> li').removeClass('hidden');
          $(this).find('a').html('Close View');
        } else {
          $list.addClass('closed').find('> li:gt(11)').addClass('hidden');
          $(this).find('a').html('View All');
        }
        
      });
    }
  
  });

  /*
   * Play Audio with Media Element
   */
  $('audio').mediaelementplayer();

  /*
   * Open external links in a new window
   */
  $("a[href^=http]").each(function() {
    if(this.href.indexOf(location.hostname.replace(/www./gi, "")) == -1) {
      // Ensure subdomains are not counted as external
      if(this.href.indexOf('tyco.com') == -1 
      && this.href.indexOf('phx.corporate') == -1
      && this.href.indexOf('corporate-ir.net') == -1) {
        
        $(this).click(function() { 
          // Record in google analytics before opening
          recordOutboundLink(this, 'Outbound Links', this.href);
          return false; 
        });
      }
    }
  });

  /* 
   *  Custom form elements using Uniform.js
   */
  if($.browser.msie && $.browser.version < 8) {
    // no input enhancments
  } else {
    $("input:checkbox, input:radio").uniform({ useID: false });
    $("select").not(".full").uniform({ useID: false});
    $("select.full").uniform({ useID: false, selectClass: 'selector full'});
  }

  /* 
   *  Placeholder text for older browsers
   */
  $('input, textarea').placeholder();

  /*
   * Vertical centering - use sparingly
   * $element.vAlign(offset)
   */
  $('.extra h3').vAlign(-7);

  /*
   * IE7 first-child fix
   * IE7 actually supports the first-child selector but html
   * comments sometimes confuses poor old IE so this is insurance.
   */
  $('.ie7 [class*="grid"]:first-child').addClass('first');

  /*
   * IE7 :before and :after pseudo element fixes for tyco slashes
   */
  // breadcrumbs
  $('.ie7 .breadcrumb li').append('<span style="padding-left:4px;color:#a5a5a5">/</span>');
  // headlines
  $('.ie7 h1.alt, .ie7 h2.alt, .ie7 h3.alt, .ie7 h4.alt').prepend('<span style="padding-right:4px;color: #212121;">//</span>');
  

});

/*
 * Function to track outbound links before opening
 * @see http://support.google.com/analytics/bin/answer.py?hl=en&answer=1136920
 */
function recordOutboundLink(link, category, action) {
  try {
    // Track click event then open link after short delay
    _gaq.push(['_trackEvent', category , action]);
    setTimeout(function(){ window.open(link.href, "_blank"); }, 100);
  } catch(err) {
    // Just open link if there's an error
    //console.log(err);
    window.open(link.href, "_blank");
  }
}
